//
//  ExampleViewModel.m
//  NekaPrice
//
//  Created by yu on 2018/6/25.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "ExampleViewModel.h"

@implementation ExampleViewModel


- (instancetype)init {
    if (self = [super init]) {
        
        _perPage = 18;
        self.showExampleList = [NSMutableArray new];
    }
    return self;
}

- (void)requestExampleList:(void (^)(void))finished {
    
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_CASE_LIST_ALL) parames:nil success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        NSArray *list = responseObj[@"caselist"];
        [self setupPageCount:list];
        
        dispatch_group_leave(group);
    } failure:^(id error) {
        dispatch_group_leave(group);
    }];
    
    if (self.categoryList.count == 0) {
        dispatch_group_enter(group);
        [[NKNetWorkTool sharedTool] requestGET:FormURL(API_CASE_CATEGORY_LIST) parames:nil success:^(id responseObj) {
            
            NSArray *list = responseObj[@"category"];
            for (NSDictionary *dic in list) {
                CategoryModel *model = [CategoryModel ModelWithDic:dic];
                [self.categoryList addObject:model];
            }
            dispatch_group_leave(group);
        } failure:^(id error) {
            dispatch_group_leave(group);
        }];
        
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        finished();
    });
  
}

- (NSMutableArray *)categoryList {
    if (_categoryList == nil) {
        _categoryList = [NSMutableArray new];
    }
    return _categoryList;
}

- (void)makeShowExampleList:(void (^)(BOOL))finished {
    
    self.currentPage ++;
    if (self.currentPage < self.allPages) {//有可加载的数据
        [self.showExampleList removeAllObjects];
        if (self.currentPage == self.allPages - 1) {//最后一页时,直接获取所有数据,避免获取范围超过实际数据量
            [self.showExampleList addObjectsFromArray:self.allExampleList];
        }else {
            [self.showExampleList addObjectsFromArray:[self.allExampleList subarrayWithRange:NSMakeRange(0, self.perPage * (self.currentPage + 1))]];
        }
        finished(YES);
    }else {//无可加载的数据
        finished(NO);
    }
}


- (void)requestExampleListByURL:(NSString *)url withBlock:(void (^)(void))finished {
    
    
    [[NKNetWorkTool sharedTool] requestGET:url parames:nil success:^(id responseObj) {
        
        NSArray *list = responseObj[@"caselist"];
        [self setupPageCount:list];
        
        finished();
    } failure:^(id error) {
        
    }];
    
}


- (void)setupPageCount:(NSArray *)list {
    
    self.currentPage = 0;
    self.allPages = 0;
    self.perPage = 18;
    
    self.allExampleList = [NSMutableArray new];
    
    for (id dic in list) {
        if ([dic isKindOfClass:[ExampleModel class]]) {//搜索的时候,list里边的是model
            [self.allExampleList addObject:dic];
        }else {//分类请求的时候,list里边是dict
            ExampleModel *model = [ExampleModel ModelWithDic:dic];
            [self.allExampleList addObject:model];
        }
        
    }
    
    //根据数据的条数 设置单页个数和页数
    if (self.self.allExampleList.count < self.perPage) {
        self.perPage = self.self.self.allExampleList.count;
        self.allPages = 1;
    }else {
        self.allPages = self.allExampleList.count / self.perPage;
    }
    
    
    [self.showExampleList removeAllObjects];
    [self.showExampleList addObjectsFromArray:[self.allExampleList subarrayWithRange:NSMakeRange(0, self.perPage * (self.currentPage + 1))]];
    
}




@end
